var list_input = Array('password','email_name','question','answer','password2','cmnd','noicap','d_cmnd','m_cmnd','y_cmnd','verify_image','hoten','dienthoai','diachi','sSex_m','sSex_f');
/**Check ngay cap ###################################################################################*/
var hoten_invalid = "Họ tên phải có ít nhất 1 ký tự chữ.";
function check_require_field(){
	//////////////////////
	/////////////////////////////
	if(trim($('#diachi').val()) == ''){
		flag = false;
		showError('#diachi','Địa chỉ không hợp lệ.');
	}
	if(trim($('#dienthoai').val()) == ''){
		flag = false;
		showError('#dienthoai','Số điện thoại không hợp lệ.');
	}	
	////////////////////////////////
	/////////////////////////	
	var frm =	document.frmRegister ;
	var list_input_require = new Array('password','email_name','password2','personalid','regionissue','d_cmnd','m_cmnd','y_cmnd','sVerifyCode','hoten','dienthoai','diachi');
	var flag = true;
	for(var i=0;i<list_input_require.length;i++){
		if($('#'+list_input_require[i]).val()  == ''){
			$('#tr_'+list_input_require[i]).addClass('tr_error');			
			showError('#'+list_input_require[i],Require_Error);
			flag = false;
		}
	}
	if($("#email_name").val() != '' ){
		if($('#sp_sel_domain').css('display') != 'none'){
			if($("#email_domain").val() == '' || $("#email_domain").val() == -1) {
				showError('#email_name',Require_Error);
				flag = false;
			}
		}
		if($('#sp_txt_domain').css('display') != 'none'){
			$('#email_domain').val(-1);
			if($("#email_domain_txt").val() == '') {
				showError('#email_name',Require_Error);
				flag = false;
			}
		}		
		
	}	
	if($("#regionissue").val() == -1) {
		showError('#regionissue',Region_Error[0]);
		flag = false;
	}
	if(!$('#sSex_m').is(':checked') && !$('#sSex_f').is(':checked')){
		showError('#sSex_m',Require_Error);
		flag = false;
	}else{
		hideError("#sSex_m");	
	}
	if($("#dName").val() == -1 && $("#mName").val() == -1 && $("#yName").val() == -1) {
		showError('#dName',Require_Error);
		flag = false;
	}
	
	var cmnd	= 	frm.elements["d_cmnd"];
	if(cmnd) {
		var d_cmnd = trim($('#d_cmnd').val());	
		var m_cmnd = trim($('#m_cmnd').val());			
		var y_cmnd = trim($('#y_cmnd').val());
		if(d_cmnd == -1 &&  m_cmnd == -1 && y_cmnd == -1){
			showError('#d_cmnd',Require_Error);
			show_cmnd();
			flag = false;
		} else{
			if(!checkDate(d_cmnd,m_cmnd,y_cmnd)){
				showError('#d_cmnd',Persional_Error[1]);
				flag = false;
			}else{
				hideError('#d_cmnd');	
			}
		}
	}
		/** Update 09/08 **/	
	var maritalstatus_1 = document.getElementById('maritalstatus_1').checked;
	var maritalstatus_2 = document.getElementById('maritalstatus_2').checked;	
	/*********/
	if(maritalstatus_1 == false && maritalstatus_2 == false){
		showError('#maritalstatus_1',Require_Error);
		flag = false;
	}else{
		hideError('#maritalstatus_1');	
	}
	var job = $('#job').val();
	if(job==-1){
		showError('#job',Require_Error);
		flag = false;
	}else{
		hideError('#job');	
	}
	var add = $('#add').val();
	if(add==-1){
		showError('#add',Require_Error);
		flag = false;
	}else{
		hideError('#add');	
	}	
	/***
	update
	**/
	return flag;
}
/**** Update maritalstatus *****/
$("#job").blur( function() {
	if(trim(this.value) != -1)	checkValid("#job_icon");
} );
$("#add").blur( function() {
	if(trim(this.value) != -1)	checkValid("#add_icon");
} );
$("#maritalstatus_1").click( function() {
	hideError("#maritalstatus_1");
} );
$("#maritalstatus_2").click( function() {
	hideError("#maritalstatus_1");
} );
$("#job").click( function() {
	hideError("#job");
} );
$("#add").click( function() {
	hideError("#add");
} );
/**********/
function hide_description(curent_input){
	for(var i=0;i<list_input.length;i++){
		if(list_input[i] != curent_input){
			$('#description_'+list_input[i]).hide();
		}
	}
	$('#tr_'+curent_input).removeClass('tr_error');
	$('#error_'+curent_input).html('');	
	$('#description_'+curent_input).show();
}
function on_submit(){
	var flag = true;	
	var flag_anser = checkSubmitConfirmAnswer1();
	if(!check_require_field() || flag_anser == false) 
	{
		return false;
	}
	else 
	{		
		//alert(2);
		var password = $('#password').val();
		var email_name = $('#email_name').val();
		var email_domain = $('#email_domain').val();		
		var question = $('#question').val();
		var answer = $('#answer').val();
		var password2 = $('#password2').val();		
		
		var cmnd = $('#cmnd').val();	
		var d_cmnd = $('#d_cmnd').val();			
		var m_cmnd = $('#m_cmnd').val();			
		var y_cmnd = $('#y_cmnd').val();
		
		var verify_image = $('#sVerifyCode').val();
		var frm =	document.frmRegister ;
		
		if(password != '') {			
			if(!checkVietNam($('#password').val())){
				showError('#password',Password_Error[0]);
				flag = false;
			} else if(!checkLen($('#password').val(),4,32)) {
				showError('#password',Password_Error[1]);
				flag = false;
			} else {
				hideError('#password');
			}
		}
		var email = frm.elements["email_name"];
		if(email) {
			if(email_name != '') {			
				if(!check_email_name('#email_name')) {
					flag = false;
				} else {
					hideError('#email_name');
				}
			}
		}
		if(!checkQuestion()) flag = false;
		var cmnd	= 	frm.elements["d_cmnd"];
		if(cmnd) {
			if(d_cmnd == -1 &&  m_cmnd == -1 && y_cmnd == -1){
				showError('#d_cmnd',Require_Error);
				show_cmnd();
				flag = false;
			} else {			
				if(!checkDate(d_cmnd,m_cmnd,y_cmnd)){
					showError('#d_cmnd',Persional_Error[1]);
					show_cmnd();
					flag = false;
				}else{
					hideError('#d_cmnd');	
				}
			}		
		}
		var verify_code	= 	frm.elements["sVerifyCode"];
		if(verify_code){
			var sVerifyCode = $('#sVerifyCode').val();
			if(sVerifyCode.length != 6){
				showError("#sVerifyCode",Verify_Error[1]);
				flag = false;
			}
			else if(!checkImageSecurity(verify_code,6,true)) {
				showError("#sVerifyCode",Verify_Error[1]);
				flag = false;
			}
		}
	}	
	if(checkStringIsNum($('#hoten').val())){
		showError("#hoten",hoten_invalid);
		flag = false;
	}
	
	if(checkDisplayError()) return false;
	act_UpdateProfiePayer_GA();
	if(flag) {
		return true;
	}
	return false;
}

function checkDisplayError(){
	var c1 = check_('password_err');
	var c2 = check_('hoten_err');
	var c3 = check_('sSex_m_err');
	var c4 = check_('dName_err');
	var c5 = check_('diachi_err');
	var c6 = check_('dienthoai_err');
	var c7 = check_('personalid_err');
	var c8 = check_('d_cmnd_err');
	var c9 = check_('regionissue_err');
	var c10 = check_('sEmailName_err');	
	var c11 = check_('question_err');	
	var c12 = check_('answer_err');		
	var c13 = check_('sVerifyCode_err');			
	if(c1 || c2 || c3 || c4 || c5 || c6 || c7 || c8 || c9 || c10 || c11 || c12 || c13){
		return true;
	}
	return false;
}
function check_(id){	
	if($('#'+id).css('display')){
		if($('#'+id).css('display') != 'none'){
			return true;
		}
	}
	return false;
}

////////////////
$("#hoten").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('hoten');
	if(this.value == ''){
		hideError('#hoten');
	}else{
	}	
} );
$("#diachi").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('diachi');
	if(this.value == ''){
		hideError('#diachi');
	}else{
	}	
} );
$("#dienthoai").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('dienthoai');
	if(this.value == ''){
		hideError('#dienthoai');
	}else{
	}	
} );
$("#sSex_m").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('sSex_m');
	hideError('#sSex_m');
} );
$("#sSex_f").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('sSex_m');
	hideError('#sSex_m');
} );

/*Check password*/
var flag_error_password = 0;
$("#password").focus( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							   
	hide_description('password');
	if(this.value == ''){
		hideError('#password');
	}else{
	}	
} );
$("#password").blur( function(){
	//Update check							 
	if(check_show_keyboard()) return true;
	//End							  
	return check_password(this);
} );
$("#password").keyup( function() {
} );
function check_password(obj){	
	if(!checkVietNam(obj.value)){
		return showError(obj,Password_Error[0]);
	}
	if(!checkLen(obj.value,4,32)) {
		return showError(obj,Password_Error[1]);
    } 
	if(obj.value) {
		checkValid("#password_icon");
	}
	return hideError(obj);
}
/*Check password*/
$("#email_name").focus( function() {
	hide_description('email_name');							 
	if(this.value == ''){
		hideError("#email_name");
		$("#email_name_tr_note").addClass("td_note");
		$("#email_name_note").show();
	}else{
		//$(des).hide();
	}	
} );
$("#email_name").blur( function() {
	$('#email_name_note').hide();
	$("#email_name_tr_note").removeClass("td_note");
	if(!check_email_name(this))
		return false;
	hideError(this);
} );
function check_email_name(obj){	
	var mailName = $("#email_name").val();
	if($('#sp_sel_domain').css('display') != 'none'){
		var mailDomain = $('#email_domain').val();
	}
	if($('#sp_txt_domain').css('display') != 'none'){
		var mailDomain = $("#email_domain_txt").val();
	}	
	if(mailDomain == '' || mailDomain == -1){
		return false;	
	}	
	if(mailDomain=='zing.vn' && mailName == $('#accLogin').val()) {
		showError(obj,'Không được trùng tên đăng nhập.');
		return false;	
	}	
	var fullName = mailName+'@'+mailDomain;
	if(!CheckEmail(mailName,mailDomain,2,4,7,100)){
		showError(obj,Email_Error[0]);
		return false;
	}
	hideError(obj);
	return true;
}
$("#email_domain").blur( function() {									
	$('#email_name_note').hide();
	$("#email_name_tr_note").removeClass("td_note");
	if(!check_email_name($("#email_name"))) {
		return false;
	}
	else {
		checkValid("#email_name_icon");
	}
} );
$("#email_domain_txt").blur( function() {									
	$('#email_name_note').hide();
	$("#email_name_tr_note").removeClass("td_note");
	if(!check_email_name($("#email_name")))
		return false;
	else
		checkValid("#email_name_icon");
} );

$("#answer").focus( function() {
	hide_description('answer');							 
	if(this.value == '')
		hideError("#answer");				
	$("#answer_tr_note").addClass("td_note");
	$("#answer_note").show();
} );
$("#answer").blur( function(){
    $('#answer_note').hide();
	$("#answer_tr_note").removeClass("td_note");
	if($("#answer").val()) {
		checkValid("#answer_icon");
	}
} );
$("#password2").focus( function() {	
	hide_description('password2');								
	if(this.value == ''){
		hideError("#password2");
		$("#password2_tr_note").addClass("td_note");
		$("#password2_note").show();		
	}else{

	}
} );
$("#password2").blur( function(){
    $('#password2_note').hide();
	return check_password2(this);
} );
function check_password2(obj){	
	if(!checkVietNam(obj.value)){
		return showError(obj,Password_Error[0]);
	}	
	if(!checkLen(obj.value,6,32)) {
		return showError(obj,Password_Error[2]);
    }
	if(obj.value) {
		checkValid("#password2_icon");
	}
	return hideError(obj);	
}
$("#password2").keyup( function() {
	if(!checkVietNam(this.value)){
		chkPassErr = 1;
		resetMeta();
		return showError(this,Password_Error[0]);
	}else{
		if($("#sPassWord1_note").css('display') == 'none'){
			chkPassErr = 0;
			hideError(this);
		}
	}
	if(this.value.length < 6){
		resetMeta();
		return false;
	}	
	var level = password_level(this.value);
	if(level){
		//level 1
		if((level[0] && !level[1] && !level[2] && !level[3])
			 || (!level[0] && level[1] && !level[2] && !level[3])
			 || (!level[0] && !level[1] && level[2] && !level[3])
			 || (!level[0] && !level[1] && !level[2] && level[3])){			
			$('#meta1').addClass("meta");			
		}
		//level 2
		if((level[0] && level[1] && !level[2] && !level[3]) 
			|| (level[0] && !level[1] && level[2] && !level[3])
			|| (level[0] && !level[1] && !level[2] && level[3])
			
			|| (!level[0] && level[1] && level[2] && !level[3])
			|| (!level[0] && level[1] && !level[2] && level[3])
			
			|| (!level[0] && !level[1] && level[2] && level[3])){
			$('#meta1').addClass("meta");
			$('#meta2').addClass("meta");			
		}else{
			$('#meta2').removeClass("meta");			
		}
		//level 3			
		if((level[0] && level[1] && level[2] && !level[3]) 
			|| (level[0] && level[1] && !level[2] && level[3])
			|| (level[0] && !level[1] && level[2] && level[3])			
			|| (!level[0] && level[1] && level[2] && level[3])){
			$('#meta1').addClass("meta");
			$('#meta2').addClass("meta");		
			$('#meta3').addClass("meta");			
		}
		else{
			$('#meta3').removeClass("meta");			
		}
		//level 4			
		if(level[0] && level[1] && level[2] && level[3]){
			$('#meta1').addClass("meta");
			$('#meta2').addClass("meta");		
			$('#meta3').addClass("meta");		
			$('#meta4').addClass("meta");			
		}else{
			$('#meta4').removeClass("meta");
		}			
	}			
} );
$("#personalid").focus( function() {								 
	if(this.value == '')
		hideError("#personalid");
	$("#personalid_tr_note").addClass("td_note");
	$("#personalid_note").show();
} );
$("#personalid").blur( function() {
	$('#personalid_note').hide();
	if(!checkPersonalId(this.value) && this.value.length > 0){
		return showError(this,Persional_Error[0]);
	}	
	if(this.value) {
		checkValid("#personalid_icon");
	}	
	hideError("#personalid");
} );

$("#regionissue").focus( function() {	
	if(this.value == -1)
		hideError("#regionissue");				
	//$("#regionissue_tr_note").addClass("td_note");
	$("#regionissue_note").show();
} );
$("#regionissue").blur( function(){
    $('#regionissue_note').hide();
	//$("#regionissue_tr_note").removeClass("td_note");
	if($("#regionissue").val()!=-1) {
		checkValid("#regionissue_icon");
	}
} );
$("#noicap").focus( function() {
	var des = '#description_noicap';
	if(this.value == ''){
		hide_description('noicap');
	}else{
		$(des).hide();
	}	
} );

function check_verify_image(){
	var verify_image = $("#sVerifyCode").val();
	if(verify_image != ''){
		if(verify_image.length != 6){
			return showError('#sVerifyCode',Verify_Error[0]);
		}else{
			hideError('#sVerifyCode');
			return true;
		}
	}
	return true;
}
$("#email_domain").change( 	
	function(){
		chkDomainSel = 1;
		if(this.value == -1){
			chkDomain = 1;
	 		$('#sp_sel_domain').hide();	
	 		$('#sp_txt_domain').show();
			$('#email_domain_txt').focus();			
		}
		
	 }
 );
//################################################# EMAIL
function domainBlur(obj){	
	var mailName = $("#sEmailName").val();
	var mailDomain = obj.value;
	var fullName = mailName+'@'+mailDomain;
	if(fullName == curAcc){
		chkEmailErr = 1;
		return showError('#sEmailName',Email_Error[2]);
	}
	if(mailDomain == '' || mailDomain == -1){
		chkEmailErr = 0;
		hideError('#sEmailName');
		return false;	 					
	}	
	if(!CheckEmail(mailName,mailDomain,2,4,7,100)){
		chkEmailErr = 1;
		return showError('#sEmailName',Email_Error[0]);
	}else{
		if(curEmailname == mailName && curDomain == mailDomain)
			return false;
		//checkEmailAjax();
		return false;
	}	
}
/* #################################### xu ly date #############################*/
var Date_invalid = Persional_Error[1];
$("#y_cmnd").blur( function() {
	return checkDateValid('yyyy','d_cmnd','m_cmnd','y_cmnd',Date_invalid,1);
} );
$("#d_cmnd").blur( function() {
	return checkDateValid('dd','d_cmnd','m_cmnd','y_cmnd',Date_invalid,1);
} );
$("#m_cmnd").blur( function() {
	return checkDateValid('mm','d_cmnd','m_cmnd','y_cmnd',Date_invalid,1);
} );
//////////////////////////////////////////////////////////////////////////
var Date_invalid_birthday = 'Ngày sinh không hợp lệ.';
$("#yName").blur( function() {
	return checkDateValid('yyyy','dName','mName','yName',Date_invalid_birthday,0);
} );
$("#dName").blur( function() {
	return checkDateValid('dd','dName','mName','yName',Date_invalid_birthday,0);
} );
$("#mName").blur( function() {
	return checkDateValid('mm','dName','mName','yName',Date_invalid_birthday,0);
} );
function checkDateValid(opt,dd,mm,yy,mess,cmnd){
	var flag = 0;
	var dd = $("#"+dd).val();	
	var mm = $("#"+mm).val();
	var yyyy = $("#"+yy).val();
	if(dd == -1 && mm==-1 && yyyy == -1){
		if(cmnd == 1)
			hideError('#d_cmnd');
		else
			hideError('#dName');
	}
	//neu blur ddd
	if(opt == 'dd'){
		if(mm != -1 && yyyy != -1){
			if(!checkDate(dd,mm,yyyy) ){
				if(cmnd == 1)
					return showError('#d_cmnd',mess);
				return showError('#dName',mess);
			}else{
				flag = 1;	
			}		
		}	
	}
	//neu blur mm
	if(opt == 'mm'){
		if(yyyy != -1){
			if(!checkDate(dd,mm,yyyy) ){
				if(cmnd == 1)
					return showError('#d_cmnd',mess);
				return showError('#dName',mess);
			}else{
				flag = 1;	
			}
		}	
	}
	//neu blur yyyy
	if(opt == 'yyyy'){
		if(dd != -1 || mm != -1){
			if(!checkDate(dd,mm,yyyy) ){
				if(cmnd == 1)
					return showError('#d_cmnd',mess);
				return showError('#dName',mess);
			}else{
				flag = 1;				
			}			
		}	
	}
	if(flag == 1){
		if(cmnd == 1){
			hideError('#d_cmnd');
			return checkValid("#d_cmnd_icon");			
		}
		hideError('#dName');
		return checkValid("#dName_icon");
	}
}
function checkStringIsNum(str){
	for(i=0;i<str.length;i++){
		if(!checkNum(str.charCodeAt(i)))
			return false;
	}
	return true;
}
///////////////////// General ////////////////////////
function checkDate(dd,mm,yyyy){     
    var mydate=new Date();
	var year=mydate.getFullYear();
	var month=mydate.getMonth() + 1;
	var daym=mydate.getDate(); 
  if(!checkStringIsNum(dd) || !checkStringIsNum(mm) || !checkStringIsNum(yyyy))   
	   	return false;
   //Kiem tra thoi gian hien tai
   if(dd < 1 || dd > 31 || yyyy < 1900)
	   	return false;
   if(yyyy > year)
   		return false;  
   if(yyyy == year){
   		if(mm > month)
			return false;  
		if(mm == month)
			if(dd > daym || dd == daym)
				return false;   
   }   
   //Kiem tra hop le
   if(!isNaN(yyyy)&&(yyyy!="")&&(yyyy<10000)){     
		if ((mm==4 || mm==6 || mm==9 || mm==11) && dd==31)
			return false; 
		if (mm == 2) { // check for february 29th
			var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));
			if (dd>29 || (dd==29 && !isleap))
				return false; 
		}
   }   
   return true;
}
//#################
$("#hoten").focus( function() {	
	if(this.value == ''){
		hideError("#hoten");	
	}
} );
$("#hoten").blur( function() {
	if(this.value == ''){
		hideError("#hoten");	
		return;
	}
	if(checkStringIsNum(this.value)){
		return showError("#hoten",hoten_invalid);
	}
	$('#hoten_note').hide();
	$("#hoten_tr_note").removeClass("td_note");
	if(trim(this.value) == "") {
		return showError("#hoten",Require_Error);
	} else if (this.value){		
		checkValid("#hoten_icon");
	}
	hideError("#hoten");
} );
$("#diachi").blur( function() {
	hideError("#diachi");
	if(trim(this.value) == ''){		
		return;
	}
	checkValid("#diachi_icon");
} );
$("#dienthoai").blur( function() {	
	if (trim(this.value) != ''){
		if(!checkVietNam_tel(this.value))
			showError('#dienthoai','Số điện thoại không hợp lệ.');
		else{
			hideError('#dienthoai');		
			checkValid("#dienthoai_icon");
		}
	}	
} );
function checkVietNam_tel(str){
	for(i=0;i<str.length;i++){
		if(str.charCodeAt(i) < 32 || str.charCodeAt(i) > 126)
			return false;			
	}
	return true;
}
function checkQuestion(){
	var question = $('#question').val();
	var answer = $('#answer').val();
	if(question){
		if(question != -1){
			if(answer == ''){
				showError('#answer','Câu trả lời không hợp lệ.');
				return false;
			}	
		}
	}
	return true;
}

$("#answer").blur( function() {	
	if(trim(this.value) != ''){				
		checkValid("#answer_icon");		
	}	
} );

function checkNum(c){
	if(c > 47 && c < 58)
		return true;
	return false;
}
function checkStringIsNum(str){
	for(i=0;i<str.length;i++){
		if(!checkNum(str.charCodeAt(i)))
			return false;
	}
	return true;
}